package com.yc.encrypt;


import javax.crypto.*;
import javax.crypto.spec.DESKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.util.Base64;

public class DesEncrypt {

    private static DesEncrypt instance;

    private Cipher encCipher;
    private Cipher decCipher;


    private String key = "HK6Jy6OrfB7ZkZg6WD/rQIZfNeFn+nrrXQCTfN7Cv3cAADoAAAAAAOPCv3cAAAAAAM35CFzU/wsIAAAAVNT/" +
            "C8LWWgwUwXwDkMB8A+GrWgzEwXwDlFzAd3AgvncAAAAAZAYAAK8nVQwUwXwDZAAAAEAAAABn0/8LXNT/C9PAWgwUwXwDQAAAAA" +
            "TBfAMJTRN4SLuBCnzBfAMAAAAAEAAAAAAAAAAAAAAAAAAAAAAAAABEWnGJorzX8xAuTW1+kKO3zOL5ESpEX3uYttX1BhgrP1Rqg" +
            "ZmyzOcDID5dfY6gs8fc8gkhOlRvi6jG5QUWKDtPO7f8CyC1YQxQvmEMAAAAAASCwBckn0F6gAAAAGjT/ws=";


    public static DesEncrypt getInstance() {
        if (instance == null) {
            synchronized (DesEncrypt.class) {
                if (instance == null) {
                    instance = new DesEncrypt();
                }
            }
        }
        return instance;
    }

    private DesEncrypt() {
        DESKeySpec dks;
        try {
            dks = new DESKeySpec(key.getBytes());
            SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
            SecretKey desKey = skf.generateSecret(dks);
            encCipher = Cipher.getInstance("DES");
            decCipher = Cipher.getInstance("DES");
            encCipher.init(Cipher.ENCRYPT_MODE, desKey);
            decCipher.init(Cipher.DECRYPT_MODE, desKey);
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (InvalidKeySpecException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        }
    }

    public String encrypt(String str) {
        byte[] utf8 = new byte[0];
        try {
            utf8 = str.getBytes("UTF8");
            byte[] enc = encCipher.doFinal(utf8);
            return java.util.Base64.getEncoder().encodeToString(enc);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        }

        return null;
    }

    public String decrypt(String str)  {
        Base64.Decoder decoder = Base64.getDecoder();
        byte[] dec = decoder.decode(str);
        byte[] utf8 = new byte[0];
        try {
            utf8 = decCipher.doFinal(dec);
            return new String(utf8, "UTF8");
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        return null;
    }
    
  public static void main(String[] args) {
	DesEncrypt e = DesEncrypt.getInstance();
	String ymc = "!Qaz2wsx";
	System.out.println("原文:" + ymc);
	String jiami = e.encrypt(ymc);
	System.out.println("加密后:" + jiami);
	String jiemi = e.decrypt("SLAvUDHBi5o=");
	System.out.println("解密后:" + jiemi);
}
}